package com.tb.controller;

import com.tb.dubbo.IShopCarService;
import com.tb.dubbo.IShopParamService;
import com.tb.pojo.ShopCar;
import com.tb.pojo.ShopParam;
import com.tb.pojo.ShopUtils;
import com.tb.pojo.User;
import com.tb.utils.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/ShopCar")
public class ShopCarController {
    @Autowired
    private IShopParamService shopParamService;

    @Autowired
    private IShopCarService iShopCarService;

    /**
     * @api {post} /ShopCar/addShopCar 添加购物车 万旭刚
     * @apiDescription 商品添加购物车 &nbsp;&nbsp;<span style="color:#D0254E">[*需要在用户登陆状态下请求]</span>
     * @apiGroup ShopCar API
     * @apiParam {String} goodsId 商品编号 &nbsp;&nbsp;<span style="color:#D0254E">[*必选字段]</span>
     * @apiParam {int}  goodsNum 商品数量 &nbsp;&nbsp;<span style="color:#D0254E">[*必选字段]</span>
     * @apiParam {String} lShopParam 规格参数 &nbsp;&nbsp;<span style="color:#D0254E">[*必选字段]</span>
     * @apiParam {ShopParam} type 类型 &nbsp;&nbsp;<span style="color:#D0254E">[*必选字段]</span>
     * @apiParam {ShopParam} text 规格 &nbsp;&nbsp;<span style="color:#D0254E">[*必选字段]</span>
     * @apiParamExample {json} 请求参数格式:
     * {
     *     "goodsId": "154790851245797",
     *     "goodsNum": 2,
     *     "lShopParam": [
     *         {
     *             "type": "尺寸",
     *             "text": "6"
     *         },
     *         {
     *             "type": "尺寸",
     *             "text": "8"
     *         },
     *         {
     *             "type": "颜色",
     *             "text": "绿色"
     *         }
     *     ]
     * }
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} 正确返回值:
     * {
     * "code": 0,
     * "msg": "",
     * "data":null
     * }
     * @apiErrorExample {json} 错误返回值:
     * {
     * "code": 1(999),
     * "msg": "参数不合法（请求超时）",
     * "data": null
     * }
     * @apiSampleRequest /ShopCar/addShopCar
     */
    //购物车新增接口 包括对购物车中商品属性的新增，
    @PostMapping(value = "/addShopCar")//http://localhost:8083/ShopCar/addShopCar
    public Msg addShopParam(@RequestBody ShopUtils su) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        HttpSession session = request.getSession();
        try {
            User user = (User) session.getAttribute("User");
            String userId = user.getUserId();
            ShopCar shopCar = new ShopCar();
            shopCar.setGoodsId(su.getGoodsId());
            shopCar.setUserId(userId);
            shopCar.setGoodsNum(su.getGoodsNum());
            /*判断当前的购物车中是否有当前商品*/
            ShopCar car = iShopCarService.selectOneShopCar(shopCar);
            if (car == null) {
                iShopCarService.insert(shopCar);
            } else {
                //如果有当前商品  就对当前商品数量加1
                iShopCarService.updateCarNum(shopCar);
            }

            List<ShopParam> shopParams = su.getlShopParam();
            for (ShopParam sp : shopParams) {
                //sp.setType(new String(sp.getType().getBytes("iso-8859-1"), "utf-8"));
                // sp.setText(new String(sp.getText().getBytes("iso-8859-1"), "utf-8"));
                sp.setGoodsId(su.getGoodsId());
                sp.setUserId(userId);
                shopParamService.insert(sp);
            }
            return Msg.success(null);
        } catch (Exception e) {
            return Msg.error(999, "请求超时，请重新登陆");
        }
    }
}
